package com.diandian.backend.store.service;

import com.diandian.backend.store.dto.StoreOfflineStoreInfoAddParamDTO;
import com.diandian.backend.store.dto.StoreOfflineStoreInfoListParamDTO;
import com.diandian.backend.store.dto.StoreOfflineStoreInfoPageParamDTO;
import com.diandian.backend.store.param.OfflineStoreBatchAddParam;
import com.diandian.backend.store.param.OfflineStoreBatchAddParamItem;
import com.diandian.backend.store.resp.StoreOfflineStoreInfoListResp;
import com.diandian.base.boot.starter.response.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OfflineStoreOpenApiService {

    private static final Logger logger = LoggerFactory.getLogger(OfflineStoreOpenApiService.class);

    @Autowired
    private StoreOfflineStoreInfoService offlineStoreService;

    public PageResult page(StoreOfflineStoreInfoPageParamDTO dataDTO){
        return offlineStoreService.page(dataDTO);
    }

    public List<String> getAllStoreCodeList(String belong, String orgId, String orgCode){

        StoreOfflineStoreInfoListParamDTO paramDTO = new StoreOfflineStoreInfoListParamDTO();
        paramDTO.setBelongEql(belong);
        paramDTO.setOrgIdEql(orgId);
        paramDTO.setOrgCodeEql(orgCode);

        List<StoreOfflineStoreInfoListResp> respList = offlineStoreService.getList(paramDTO);
        if(respList == null || respList.size() == 0){
            return new ArrayList<>();
        }

        List<String> resultList = new ArrayList<>();
        for (int i = 0; i < respList.size(); i++) {
            if(respList.get(i) != null && respList.get(i).getCode() != null && !"".equals(respList.get(i).getCode()) && !resultList.contains(respList.get(i).getCode())){
                resultList.add(respList.get(i).getCode());
            }
        }

        return resultList;
    }

    public void batchSyncStore(OfflineStoreBatchAddParam param){

        param.check();
        List<OfflineStoreBatchAddParamItem> storeDataList = param.getStores();
        String belong = param.getBelong();
        String orgId = param.getOrgId();
        String orgCode = param.getOrgCode();

        // 校验数据是否存在重复
        List<String> storeCodeList = new ArrayList<>();
        List<OfflineStoreBatchAddParamItem> filterList = new ArrayList<>();
        for (int i = 0; i < storeDataList.size(); i++) {
            String storeCode = storeDataList.get(i).getCode();
            if(storeCodeList.contains(storeCode)){
                continue;
            }

            storeCodeList.add(storeCode);
            filterList.add(storeDataList.get(i));
        }
        if(filterList.size() == 0){
            logger.info("有效数据为空，停止后续处理");
            return;
        }

        // 获取所有门店编码集合
        List<String> allStoreCodeList = getAllStoreCodeList(belong, orgId, orgCode);
        if(allStoreCodeList == null){
            allStoreCodeList = new ArrayList<>();
        }

        // 分流出新增和更新的数据
        List<StoreOfflineStoreInfoAddParamDTO> saveList = new ArrayList<>();
        List<StoreOfflineStoreInfoAddParamDTO> updateList = new ArrayList<>();
        for (int i = 0; i < filterList.size(); i++) {

            StoreOfflineStoreInfoAddParamDTO itemDTO = filterList.get(i).toStoreOfflineStoreInfoAddParamDTO();
            itemDTO.setBelong(belong);
            itemDTO.setOrgId(orgId);
            itemDTO.setOrgCode(orgCode);

            String storeCode = filterList.get(i).getCode();
            if(allStoreCodeList.contains(storeCode)){
                itemDTO.setUpdateUserId(filterList.get(i).getCreateUserId());
                itemDTO.setUpdateUserName(filterList.get(i).getCreateUserName());
                updateList.add(itemDTO);
            } else {
                itemDTO.setCreateUserId(filterList.get(i).getCreateUserId());
                itemDTO.setCreateUserName(filterList.get(i).getCreateUserName());
                saveList.add(itemDTO);
            }
        }
        logger.info("待新增数量：{}，待更新数量：{}", saveList.size(), updateList.size());

        // 执行更新
        if(updateList.size() > 0){
            offlineStoreService.batchUpdate(updateList);
        }

        // 执行保存
        if(saveList.size() > 0){
            offlineStoreService.batchAdd(saveList);
        }

    }

}
